<?php
namespace app\index\model;
use think\Exception;
use think\Model;

class Cart extends Model{
    protected $pk='rec_id';
     public static function   cartListInfo($id){
         $list=Cart::where('user_id','=',$id)->select()->toArray();
         return $list;
     }
     public static function   cartListAddInfo($get,$id){
            $goods_id=Cart::where('user_id','=',$id)->column('goods_id');
            if (in_array($get['id'],$goods_id)){
                try{
                    $find=Cart::where('user_id','=',$id)->where('goods_id','=',$get['id'])->find()->toArray();
                    $find['num']=$find['num']+1;
                   $res=Cart::where('user_id','=',$id)->where('goods_id','=',$get['id'])->update(['num'=> $find['num']]);
                }catch (Exception $e){
                    $res=false;
                }
            }else{
                try{
                    $find=Goods::where('goods_id','=',$get['id'])->find()->toArray();
                    $res=Cart::insert([
                        'user_id'=>$id,
                        'goods_id'=>$get['id'],
                        'goods_name'=>$find['goods_name'],
                        'price'=>$find['price'],
                        'num'=>1,
                        'goods_image'=>$find['default_image'],
                        'vip'=>$find['vip']
                    ]);
                }catch (Exception $e){
                    $res=false;
                }
            }
            return $res;

         }

     public static function   cartListNumInfo($post){
         try{
             $res=Cart::where('rec_id','=',$post['cid'])->update(['num'=>$post['getV']]);
         }catch (Exception $e){
             $res=false;
         }
            return $res;

          }


       public static function   cartListDelInfo($post){
         try{
             $res=Cart::destroy($post['cid']);
         }catch (Exception $e){
             $res=false;
         }
             return $res;
           }

        public static function   cartListOrder($get){
         $res=Cart::where('rec_id','in',$get)->select()->toArray();
         return $res;

            }

         public static function   cartListDel($get){
         try{
            $cart=Cart::destroy($get);
         }catch (Exception $e){
             $cart=false;
         }
         return $cart;
         }



}
